package com.gears_software.bbnetwork.activitys;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.content.Intent;
import android.view.Window;
import android.widget.EditText;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.ToastUI;

@EActivity(R.layout.activity_input_recommend)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class InputSecommendCodeActivity extends Activity {
	 
	@ViewById
	EditText edit_code;//推荐码
	
	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;
	
	@Bean
	ToastUI toast;
	
	@Pref
	BBNetworkSharedPref_ sp;
	
	//跳过
	@Click
	void skip(){
		getSkip();
	}
	
	@Background
	void getSkip(){
		ResultModel<String> res = finder.getSkipCode(sp.userId().get());
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			Intent intent = new Intent(this,Main2Activity_.class);
			startActivity(intent);
			finish();
		}else{
			showTxt("跳过失败！"+res.getMessage());
		}
	}
	
	
	//确定
	@Click
	void btn_confirm(){
		String code = edit_code.getText().toString().replace(" ", "");
		if(code==null||code.equals("")){
			showTxt("推荐码不能为空！");
			return;
		}
		confirm(code);
	}
	
	@Background
	void confirm(String code){
		ResultModel<String> res = finder.getSubmitCode(sp.userId().get(), code);
		if(res.getReturnCode().equals(ReturnCode.SUCCEED)){
			showTxt("保存成功！");
			Intent intent = new Intent(this,Main2Activity_.class);
			startActivity(intent);
			finish();
		}else{
			showTxt("保存推荐码失败！"+res.getMessage());
		}
	}
	
	@UiThread
	void showTxt(String txt){
		toast.ShowToast(txt);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AVAnalytics.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}
}
